package com.inossem.warehousestorage_inossem.adapter.changeStorage;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageManageBean;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class ChangeStorageFactoryListAdapter extends BaseAdapter {

    private Context mContext;
    private List<ChangeStorageManageBean.DataBean.FactoryListBean> mFactoryList;
    private LayoutInflater inflater;
    private ChangeStorageFactoryListAdapterListener mchangeStorageFactoryListAdapterListener;

    public ChangeStorageFactoryListAdapter(Context context, List<ChangeStorageManageBean.DataBean.FactoryListBean> factoryList, ChangeStorageFactoryListAdapterListener changeStoragefactoryListAdapterListener) {
        mContext = context;
        mFactoryList = factoryList;
        mchangeStorageFactoryListAdapterListener = changeStoragefactoryListAdapterListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mFactoryList.size();
    }

    @Override
    public Object getItem(int position) {
        return mFactoryList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChangeStorageManageBean.DataBean.FactoryListBean factoryItem = mFactoryList.get(position);
        ViewHolder viewHolder;
        if (convertView == null || (convertView != null && convertView.getTag() == null)) {
            convertView = inflater.inflate(R.layout.view_in_storage_select, null);
            viewHolder = new ViewHolder();
            //初始化控件
            initView(viewHolder, convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //初始化数据
        initData(mContext, viewHolder, convertView, factoryItem);
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.text = (TextView) view.findViewById(R.id.text);
        viewHolder.value = (TextView) view.findViewById(R.id.value);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, ChangeStorageManageBean.DataBean.FactoryListBean factoryItem) {
        viewHolder.text.setText(factoryItem.getFactoryCode() + " - " + factoryItem.getFactoryDescription());
        viewHolder.value.setText(factoryItem.getFactoryCode());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                mchangeStorageFactoryListAdapterListener.onSelect(factoryItem.getFactoryDescription(), factoryItem.getFactoryCode(), factoryItem.getInventoryAddressList());
            }
        });
    }

    private class ViewHolder {
        private TextView text, value;
    }

    public static abstract class ChangeStorageFactoryListAdapterListener {
        public abstract void onSelect(String description, String value, List<ChangeStorageManageBean.DataBean.InventoryAddressListBean> inventoryAddressList);
    }
}
